<template>
  <el-card style="margin:20px;">
    <el-tabs v-model="activeName">
      <el-tab-pane label="登录账号设置" name="loginInfo">
        <el-form
          :rules="rules"
          ref="userInfo"
          :model="userInfo"
          label-width="80px"
          style="width:350px;margin:40px auto;"
        >
          <el-form-item prop="username" label="姓名：">
            <el-input v-model="userInfo.username"></el-input>
          </el-form-item>
          <el-form-item prop="password" label="密码：">
            <el-input type="password" v-model="userInfo.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="updateUserInfo" type="primary">更新</el-button>
            <el-button @click="$router.back()">取消</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="个人详情" name="userInfo">
        <user-info />
      </el-tab-pane>
      <el-tab-pane label="岗位信息" name="jobInfo">
        <job-info />
      </el-tab-pane>
    </el-tabs>
  </el-card>
</template>

<script>
// 获取用户基本资料
import { getUserInfoAvatar } from '@/api/user'
// 更新员工基本信息
import { updateEmpInfo } from '@/api/employees'
// 个人信息、岗位信息
import userInfo from './components/user-info.vue'
import jobInfo from './components/job-info.vue'
export default {
  name: 'detail',
  //   局部组件注册
  components: {
    userInfo,
    jobInfo
  },
  data () {
    return {
      activeName: 'loginInfo',
      // 用户信息
      userInfo: {},
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.getUserInfo()
  },
  methods: {
    //   用户基本资料
    async getUserInfo () {
      let res = await getUserInfoAvatar(this.$route.query.id)
      // console.log(res)
      this.userInfo = res.data
    },
    // 更新按钮：
    updateUserInfo () {
      // 整个表单验证
      this.$refs.userInfo.validate(async v => {
        if (v) {
          let res = await updateEmpInfo(this.userInfo)
          console.log(res)
          this.$message.success('更新成功！')
          //   this.$router.push('/employees')
        }
      })
    }
  }
}
</script>

<style></style>
